local Msg = require("core.Msg")
local Obj = require("core.Obj")
local Timer = require("core.Timer")
local RoleDefine = require("role.RoleDefine")
local Skill = require("skill.Skill")
local EffectLogic = require("scene.EffectLogic")
local CombatCalc = require("skill.CombatCalc")

list = list or {}

function create(bodyID,bottomID,skill_id,skill_level,atk,aoeSpeed)
	Timer.aoeChange = true
	local magic = {}
	Obj.create(magic, Obj.TYPE_MAGIC)
	magic.bodyID = bodyID
	magic.bottomID = bottomID
	magic.skill_id = skill_id
	magic.skill_level = skill_level
	magic.atk = atk
	CombatCalc.initAttr(magic)
	CombatCalc.calcAttr(magic)
	magic.aoeSpeed = aoeSpeed * 1000
	magic.lastTime = 0
	list[magic.id] = magic
	return magic
end

function getObjAdd(magic)
	local mm = Msg.gc.GC_ADD_AOE
	mm.speed = magic.speed or 0
	mm.owner_uuid = ""
	mm.owner_combatSide = 0
	mm.owner_combatMode = 0
	mm.obj_id = magic.id
	mm.bodyID = magic.bodyID
	mm.show = 1
	mm.bottomID = magic.bottomID
	mm.bodyEffectConfig =  EffectLogic.getConfig(magic.bodyID)
	mm.bottomEffectConfig =  EffectLogic.getConfig(magic.bottomID)
	mm.comboType = 0
	mm.comboArg1 = 0
	mm.comboArg2 = 0
	mm.x = magic.x
	mm.y = magic.y
	mm.isDie =0
	mm.dir = 0
	mm.onlyShowOwner = 0
	local len = magic.path_len or 0
	mm.points[0] = len * 2
	for i = 1, len do
		local j = len + 1 - i
		mm.points[2 * i - 1] = magic.path[2 * j - 1]
		mm.points[2 * i] = magic.path[2 * j]
	end
	
	return mm
end


function getAttr(magic, key)
	if key==RoleDefine.ATK then
		return magic.atk
	end
	return 1
end

function getHp()
	return 1
end

function setHp()
end

local empty = {}
function getBuf()
	return empty,0
end
function setBufLen()
end


function getName()
	return "magic_name"
end

function destroy(magic)
	magic.die = 1
	list[magic.id] = nil
	Obj.destroy(magic)
end

function onTimer()
	for id,magic in pairs(list) do
		if magic.dieTime and Timer.now>=magic.dieTime then
			destroy(magic)
		else
			if Timer.now - magic.lastTime >= magic.aoeSpeed then
				magic.lastTime = Timer.now
				if 0 < magic.skill_id then
					Skill.add(magic,magic.skill_id,magic.x,magic.y,magic.x,magic.y,nil,magic.skill_level)
				end
			end			
		end
	end	
end